﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinFormsApp_SetCursorPos
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        
        }

        [DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")]
        [return: MarshalAsAttribute(UnmanagedType.Bool)]
        public static extern bool SetCursorPos(int x, int y);

        //该实例运行效果为 鼠标随机在屏幕内跳动
        void DoAction()
        {
            Random r = new Random(unchecked((int)DateTime.Now.Ticks));
            int i = 1;
            do
            {
                int x = r.Next(0, Screen.PrimaryScreen.Bounds.Width);
                int y = r.Next(0, Screen.PrimaryScreen.Bounds.Height);
                SetCursorPos(x, y);
                i++;
                System.Threading.Thread.Sleep(500);

            } while (i < 10);
            //Console.ReadKey();

            //^_^,如果你写个随机数无限循环,就可以让别人永远无法使用鼠标，除非关机。自己试的时候别写死循环，小心关不掉。

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            DoAction();
        } 
    }
}
